﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BidFunLib.BidWeb.Sale;
using BidFunLib.BidWeb;
using System.Collections;

namespace BidFunSpy
{
    public partial class CurrentBidDetail : Form
    {
        BidWebSale Sale { get; set; }

        Timer myTimer;

        public CurrentBidDetail(BidWebSale sale)
        {
            InitializeComponent();
            this.Text += " : " + sale.Sale.bidObject.Label + " [ " + sale.Sale.BidFunIdentifier+ " ]";
            myTimer = new Timer();
            myTimer.Interval = 500;
            myTimer.Tick += new EventHandler(myTimer_Tick);
            Sale = sale;

            Sale.OnLoggerUpdate += new OnLoggerUpdateEventHandler(Sale_OnLoggerUpdate);
            Sale.OnBrowserChange += new OnBrowserChange(Sale_onBrowserChange);

            foreach (BidUserWeb uw in Sale.MapBidUserWeb.Values)
            {
                ListViewItem itemToUpdate = new ListViewItem();
                itemToUpdate.Text = uw.User.Pseudo;
                itemToUpdate.SubItems.Add(uw.NbBidInSale.ToString());
                itemToUpdate.SubItems.Add(uw.Amount);
                itemToUpdate.SubItems.Add("");
                itemToUpdate.SubItems.Add(calculateRatio(uw));
                listView1.Items.Add(itemToUpdate);
            }
            listView1.ListViewItemSorter = new ListViewItemComparer(2, SortOrder.Descending);
            listView1.Sort();
            myTimer.Start();
        }

        void Sale_onBrowserChange()
        {
            splitContainer1.Panel2Collapsed = true;
            splitContainer1.Panel2.Controls.Clear();
        }

        private String calculateRatio(BidUserWeb uw)
        {
            Double res = 0;
            Double.TryParse(uw.Amount.Replace(".", ",").Replace("€", ""), out res);
            Double tmp =100 * ((0.75 * uw.NbBidInSale + res) / Sale.Sale.bidObject.RealPriceAsDouble);
            return tmp.ToString();
        }

        void myTimer_Tick(object sender, EventArgs e)
        {
            textBox_Time.Text = Sale.Span_time;
            textBox_nbUser.Text = Sale.NBUserConnected;
        }

        void Sale_OnLoggerUpdate(BidFunLib.Entity.BidLogger log)
        {
            ListViewItem itemToUpdate = null;
            BidUserWeb uw = null;
            foreach(ListViewItem item in listView1.Items)
            {
                if (item.Text == Sale.LastUserName)
                {
                    itemToUpdate = item;
                    break;
                }
            }
            if (itemToUpdate != null)
            {
                uw = Sale.MapBidUserWeb[Sale.LastUserName];
                itemToUpdate.SubItems[1].Text = uw.NbBidInSale.ToString();
                itemToUpdate.SubItems[2].Text = uw.Amount;
                itemToUpdate.SubItems[3].Text = log.BidOperatorType.Name.ToString();
                itemToUpdate.SubItems[4].Text = calculateRatio(uw);
            }
            else
            {
                uw = Sale.MapBidUserWeb[Sale.LastUserName];
                itemToUpdate = new ListViewItem();
                itemToUpdate.Text = Sale.LastUserName;
                itemToUpdate.SubItems.Add(uw.NbBidInSale.ToString());
                itemToUpdate.SubItems.Add(uw.Amount);
                itemToUpdate.SubItems.Add(log.BidOperatorType.Name);
                itemToUpdate.SubItems.Add(calculateRatio(uw));

                listView1.Items.Add(itemToUpdate);
            }
            listView1.Sort();
            
            
        }

        private void CurrentBidDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            Sale.OnLoggerUpdate -= Sale_OnLoggerUpdate;
            myTimer.Tick -= myTimer_Tick;
            Sale.OnBrowserChange -= Sale_onBrowserChange;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Sale.Pause();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (splitContainer1.Panel2.Controls.Count == 0)
            {
                Sale.Browser.Dock = System.Windows.Forms.DockStyle.Fill;
                if (Sale.Browser != null)
                {
                    splitContainer1.Panel2.Controls.Add(Sale.Browser);
                    splitContainer1.Panel2Collapsed = false;
                }
            }
            else
            {
                splitContainer1.Panel2Collapsed = true;
                splitContainer1.Panel2.Controls.Clear();
            }
        }

        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            listView1.ListViewItemSorter = new ListViewItemComparer(e.Column, SortOrder.Descending);
            listView1.Sort();
        }
    }
}

class ListViewItemComparer : IComparer
{
    private int col;
    private SortOrder order;
    public ListViewItemComparer()
    {
        col = 0;
        order = SortOrder.Ascending;
    }
    public ListViewItemComparer(int column, SortOrder order)
    {
        col = column;
        this.order = order;
    }
    public int Compare(object x, object y) 
    {
        int returnVal= -1;
        returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
                                ((ListViewItem)y).SubItems[col].Text);
        // Determine whether the sort order is descending.
        if (order == SortOrder.Descending)
            // Invert the value returned by String.Compare.
            returnVal *= -1;
        return returnVal;
    }
}
